package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper flavorMapper;
    @Override
    public void saveDish(DishDto dishDto) {
        //调用菜品添加
        dishMapper.insert(dishDto);
        //通过综合表拿到口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //判断是否选择了口味
        if(flavors!=null&&flavors.size()>0){
            //拿到口味的id
            Long dishId = dishDto.getId();
            for (DishFlavor flavor : flavors) {
                //把id塞到每个口味里
                flavor.setDishId(dishId);
                //调用口味添加
                flavorMapper.insert(flavor);
            }
        }
    }

    @Override
    //回显数据
    public DishDto selectDish(Long id) {
        //根据菜品id查到菜品
        Dish dish = dishMapper.selectById(id);
        //where条件
        QueryWrapper<DishFlavor> qw=new QueryWrapper<>();
        //根据菜品id拿到口味数据
        qw.lambda().eq(DishFlavor::getDishId,id);
        //根据菜品id拿到口味数据
        List<DishFlavor> dishFlavors = flavorMapper.selectList(qw);
        //新建一个综合表
        DishDto dishDto=new DishDto();
        //把查到的菜品拷贝到综合表
        BeanUtils.copyProperties(dish,dishDto);
        //因为综合表中有口味集合和setget方法直接set放入
        dishDto.setFlavors(dishFlavors);
        //返回综合表
        return dishDto;
    }

    @Override
    //修改菜品
    public void updateCai(DishDto dishDto) {
       //拿到前端传过来菜品id
        Long dishDtoId = dishDto.getId();
        //条件控制 找到口味删除
        QueryWrapper<DishFlavor> qw=new QueryWrapper<>();
        //删除口味数据
        qw.lambda().eq(DishFlavor::getDishId,dishDtoId);
        flavorMapper.delete(qw);

        //修改菜品数据
        dishMapper.updateById(dishDto);
        //拿到新写入的菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //判断写没写口味
        if(flavors!=null&&flavors.size()>0){
            //写了的话可能不止一个口味所以遍历一下
            for (DishFlavor flavor : flavors) {
                //写入数据库
                flavor.setDishId(dishDtoId);
                flavorMapper.insert(flavor);
            }
        }
    }

    @Override
    //逻辑删除方法 参数id是菜品集合因为有可能批量删除
    public void delectCai(List<Long> ids) {
        //遍历集合
        for (Long id : ids) {
            //where 条件
            QueryWrapper<Dish> qw=new QueryWrapper();
            //根据id删除菜品
            qw.lambda().eq(Dish::getId,id);
            int i = dishMapper.delete(qw);
            /*//where 条件
            QueryWrapper<DishFlavor> qw1=new QueryWrapper<>();
            //根据id删除口味
            qw1.lambda().eq(DishFlavor::getDishId,id);
            flavorMapper.delete(qw1);*/
        }
    }

    @Override
    //改变停售起售状态【status是传来的状态,ids是菜品id集合】
    public void sellAndNotSell(Integer status, List<Long> ids) {
        for (Long id : ids) {
            //where 条件 根据id查寻出来菜品
            QueryWrapper<Dish> qw=new QueryWrapper<>();
            qw.lambda().eq(Dish::getId,id);
            Dish dish = dishMapper.selectById(id);
            //修改菜品属性
            dish.setStatus(status);
            //把修改了的属性 传入菜品mapper修改方法
            dishMapper.update(dish,qw);
        }
    }
}
